define(['./../initial'], function() {
    var app = angular.module('App.prjStatus');
    app.factory('prjStatusChartModalHelper', ['$compile', '$rootScope', '$timeout', '$q', '$window',
        function($compile, $rootScope, $timeout, $q, $window) {

            // angular.element($window).bind('resize', function() {
            //     if (helper.element) {
            //         $timeout(function() {
            //             helper.open();
            //         });
            //     }
            // });

            var helper = {
                element: null,
                open: function(prj) {
                    var defer = $q.defer();
                    helper.close().then(function() {
                        var div = document.createElement('div');
                        $rootScope.markerCurPrj = prj;
                        div.setAttribute('data-prj-Status-chart-Modal', '');
                        div.setAttribute('data-marker-Cur-Prj', 'markerCurPrj');
                        div.id = "status-chart-container";
                        var element = angular.element(div);
                        document.body.appendChild(div);
                        $compile(element)($rootScope);
                        helper.element = element;
                        defer.resolve();
                    });
                    return defer.promise;
                },
                close: function() {
                    var defer = $q.defer();
                    if (helper.element) {
                        $('#status-chart-container').removeClass('show').addClass('hide');
                        $timeout(function() {
                            if (helper.element) helper.element.remove();
                            helper.element = null;
                            defer.resolve();
                        }, 500);
                    } else {
                        defer.resolve();
                    }
                    return defer.promise;
                }
            };
            return helper;
        }
    ]);
});
